package zhoug.zui.popup;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import zhoug.common.adapters.BaseListViewAdapter;
import zhoug.logging.Logger;
import zhoug.zui.R;
import zhoug.zui.group.ZUIFrameLayout;
import zhoug.zui.ints.OnSelectedListener;

import java.util.List;

import androidx.annotation.DrawableRes;
import androidx.core.widget.ImageViewCompat;

/**
 * 单选列表popupWindow
 * @Author 35574
 * @Date 2020/7/28
 * @Description
 */
public abstract class ZUISelectListPopupWindow<T> extends ZUIBasePopupWindow {
    private static final String TAG = ">>>ZUISelectListPopup";
    private static final boolean DEBUG=false;
    private List<T> mData;
    private Adapter mAdapter;
    private ZUIFrameLayout mContentView;
    private ListView mListView;
    private Drawable divider;
    private int dividerHeight;
    /**
     * 选择的item position
     */
    private int selected = -1;
    /**
     * 选择监听
     */
    private OnSelectedListener onSelectedListener;
    /**
     * 勾选图标
     */
    private @DrawableRes int iconDrawableRes;
    /***
     * tint color
     */
    private ColorStateList tint;


    public ZUISelectListPopupWindow(Context context) {
        this(context, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public ZUISelectListPopupWindow(Context context, int width, int height) {
        super(context, width, height);
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZUISelectListPopupWindow, R.attr.ZUISelectListPopupWindow_theme, 0);
        iconDrawableRes = a.getResourceId(R.styleable.ZUISelectListPopupWindow_zui_iconDrawable, 0);
        tint = a.getColorStateList(R.styleable.ZUISelectListPopupWindow_zui_tint);
        a.recycle();
        init();
    }

    /**
     * 初始化
     */
    private void init() {
//        setAnimationStyle(ZUIAnimations.ANIM_ANCHOR_DOWN);
        mContentView = (ZUIFrameLayout) LayoutInflater.from(getContext()).inflate(R.layout.zui_list_popup, null);
        setContentView(mContentView);
        mListView = mContentView.findViewById(R.id.listView);

        if (DEBUG) {
            Logger.d(TAG, "init:getWidth()="+getWidth());
        }

        mListView.setDivider(null);
        mListView.setDividerHeight(0);

        mAdapter=new Adapter();
        mListView.setAdapter(mAdapter);
    }

    private class  Adapter extends BaseListViewAdapter<T>implements View.OnClickListener {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null){
                convertView= LayoutInflater.from(getContext()).inflate(R.layout.zui_select_list_item,parent,false);
                ImageView imageView = convertView.findViewById(R.id.imageView);
                if (iconDrawableRes != 0) {
                    imageView.setImageResource(iconDrawableRes);
                }
                if (tint != null) {
                    ImageViewCompat.setImageTintList(imageView, tint);
                }
            }
            TextView textView=convertView.findViewById(R.id.textView);
            ImageView imageView=convertView.findViewById(R.id.imageView);
            T itemData = getItemData(position);
            if (itemData != null) {
                textView.setText(getText(itemData));
                if (selected == position) {
                    imageView.setVisibility(View.VISIBLE);
                } else {
                    imageView.setVisibility(View.GONE);
                }
                convertView.setTag(position);
                convertView.setOnClickListener(this);
            } else {
                textView.setText(null);
                imageView.setVisibility(View.GONE);
                convertView.setOnClickListener(null);
            }


            return convertView;
        }

        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            if (selected != position) {
                selected = position;
                notifyDataSetChanged();
                if (onSelectedListener != null) {
                    onSelectedListener.onSelected(selected);
                }
            }
            dismiss();
        }
    }

    /**
     * 获取指定item项的数据
     * @param position 位置
     * @return T
     */
    public T getItemData(int position) {
        if (mData != null && position>=0 && position<mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    /**
     * 滚动到指定位置
     *
     * @param position
     */
    protected void scrollToSelected(int position) {
        if (mListView != null && getData() != null && getData().size() > 0) {
            if(DEBUG){
                Log.d(TAG, "initListView:设置选中位置:" + position);
            }
            if (position >= 0 && position < getData().size()) {
                mListView.setSelection(position);
            } else {
                mListView.setSelection(0);
            }
        }
    }

    /**
     * 显示的文本
     * @param itemData
     * @return
     */
    public abstract String getText(T itemData);

    /**
     * 选择监听
     * @param onSelectedListener
     */
    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    /**
     * 选择的item的位置
     * @return
     */
    public int getSelected() {
        return selected;
    }

    /**
     * 设置选择项
     * @param selected 0开始
     */
    public void setSelected(int selected) {
        this.selected = selected;
    }


    /**
     * 设置选择图片
     * @param iconDrawableRes
     */
    public void setIconDrawableRes(int iconDrawableRes) {
        this.iconDrawableRes = iconDrawableRes;
    }

    /**
     * 设置图片颜色
     * @param tint
     */
    public void setTint(ColorStateList tint) {
        this.tint = tint;
    }

    /**
     * 设置最大高度
     * @param maxHeight
     */
    public void setMaxHeight(int maxHeight){
        mContentView.setMaxHeight(maxHeight);
    }

    /**
     * 是否显示分割线:分割线高度和颜色需要在之前设置
     * @param showDivider
     * @return
     */
    public ZUISelectListPopupWindow<T> showDivider(boolean showDivider){
        if(showDivider){
            if(divider==null){
                divider=new ColorDrawable(Color.parseColor("#dddddd"));
            }
            if(dividerHeight==0){
                dividerHeight=1;
            }
            mListView.setDivider(divider);
            mListView.setDividerHeight(dividerHeight);
        }else{
            mListView.setDivider(null);
            mListView.setDividerHeight(0);
        }
        return this;
    }

    /**
     * 分割线
     * @param divider
     * @return
     */
    public ZUISelectListPopupWindow<T> setDivider(Drawable divider){
        this.divider = divider;
        return this;
    }

    /**
     * 分割线高度
     * @param dividerHeight
     * @return
     */
    public ZUISelectListPopupWindow<T> setDividerHeight(int dividerHeight){
        this.dividerHeight = dividerHeight;
        return this;
    }

    public void setData(List<T> data) {
        this.mData = data;
        mAdapter.setData(mData);
    }

    public List<T> getData() {
        return mData;
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        scrollToSelected(selected);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        scrollToSelected(selected);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        super.showAsDropDown(anchor, xoff, yoff);
        scrollToSelected(selected);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        scrollToSelected(selected);
    }
}
